package com.lb.jwitter.hibernate;

import org.apache.commons.logging.Log;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.math.BigInteger;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

import javax.persistence.Transient;

import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;
import com.lb.utils.orm.hibernate.HibernateGenericDao;
import org.springframework.transaction.annotation.Transactional;
import com.lb.jwitter.Tbl_DKMH_DotDK_Nhom;

@Repository("tbl_DKMH_DotDK_NhomDAO")
public class Tbl_DKMH_DotDK_NhomHibernateDAO extends HibernateGenericDao<Tbl_DKMH_DotDK_Nhom, Long> implements Tbl_DKMH_DotDK_NhomDAO {
   private Log log = LogFactory.getLog(Tbl_DKMH_DotDK_NhomHibernateDAO.class);

   @Autowired
   public Tbl_DKMH_DotDK_NhomHibernateDAO(@Qualifier("sessionFactory")   SessionFactory sessionFactory) {
       super(sessionFactory);
   }

   @SuppressWarnings("unchecked")
@Transactional
   public final List<Tbl_DKMH_DotDK_Nhom> findAll(int pageSize, int page) {
       if( pageSize == 0) return getSession().createCriteria(Tbl_DKMH_DotDK_Nhom.class).list(); 
       return getSession().createCriteria(Tbl_DKMH_DotDK_Nhom.class).setFirstResult(pageSize * (page-1)).setMaxResults(pageSize).list();
   }
   
@Transactional
   public final Long save(Tbl_DKMH_DotDK_Nhom _newInstance) {
   	return super.save(_newInstance);
   }

@Transactional
   public final boolean exists(Long id) {
       boolean result = (Integer) getSession().createCriteria(Tbl_DKMH_DotDK_Nhom.class).setProjection(Projections.rowCount()).add(
                       Restrictions.eq("id", id)).uniqueResult() == 1;
       log.info("Result: "+result);
   	return result;
   }

@Transactional
   public Collection<Tbl_DKMH_DotDK_Nhom> find(Tbl_DKMH_DotDK_Nhom obj,int pageSize,int page){
   	Collection<Tbl_DKMH_DotDK_Nhom> _tbl_DKMH_DotDK_Nhoms = new ArrayList<Tbl_DKMH_DotDK_Nhom>();
   	try {
   		String sql = "select d from tbl_DKMH_DotDK_Nhom as d where 1=1 ";
   sql += " and d.id = " + obj.getId() + " ";
       if(obj.getMa() != null && !obj.getMa().equalsIgnoreCase("")){
   		sql +=  " and d.ma like '%" +obj.getMa() + "%' " ;
   }
       if(obj.getTen() != null && !obj.getTen().equalsIgnoreCase("")){
   		sql +=  " and d.ten like '%" +obj.getTen() + "%' " ;
   }
   sql += " and d.dotDKId. = " + obj.getDotDKId().getId() + " ";
   if (obj.getNgayBatDau() != null)
   {
       DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
       sql += " and d.ngayBatDau = '" + df.format(obj.getNgayBatDau()) + "' ";
   }
   if (obj.getNgayKetThuc() != null)
   {
       DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
       sql += " and d.ngayKetThuc = '" + df.format(obj.getNgayKetThuc()) + "' ";
   }
   sql += " and d.Quyen = " + obj.getQuyen() + " ";
   		sql += " order by d.id DESC ";
   		_tbl_DKMH_DotDK_Nhoms = getSession().createQuery(sql).setFirstResult(pageSize*(page-1)).setMaxResults(pageSize).list();
   	} catch (Exception e) {
   		e.printStackTrace();
   	}
   	return _tbl_DKMH_DotDK_Nhoms;
   }


   @Transactional
   public final Boolean CalSPDeleteChiTietHVDangKyTheoNhomDK( Long  iNhomDKId ){
		try {
			BigInteger nhomid = new BigInteger(iNhomDKId.toString());
			Query query =  getSession().createSQLQuery("CALL DeleteChiTietHVDangKyTheoNhomDK (:iNhomDKId)");
			query.setBigInteger("iNhomDKId", nhomid);
			query.executeUpdate();
			
		} catch (Exception e) {
			return false;
		}

		return true;
	
   }

   @Transactional
   public final Boolean CalSPUpdateChiTietHVDangKyTheoNhomDK( Long  iNhomDKId ){
		try {
			BigInteger nhomid = new BigInteger(iNhomDKId.toString());
			Query query =  getSession().createSQLQuery("CALL UpdateChiTietHVDangKyTheoNhomDK (:iNhomDKId)");
			query.setBigInteger("iNhomDKId", nhomid);
			query.executeUpdate();
			
		} catch (Exception e) {
			return false;
		}

		return true;
	
   }
   @Transactional
   public final Boolean CalSPCreateChiTietHVDangKyTheoNhomDK(
		   Long iDotDKId, Long  iNhomDKId , int  iTuKhoaHoc , int  iDenKhoaHoc , int  iTuSoTCTL ,	 int  iDenSoTCTL  ){
		
		try {
			BigInteger nhomid = new BigInteger(iNhomDKId.toString());
			BigInteger dotDkId = new BigInteger(iDotDKId.toString());
			Query query =  getSession().createSQLQuery("CALL CreateChiTietHVDangKyTheoNhomDK (:iDotDKId, :iNhomDKId , :iTuKhoaHoc , :iDenKhoaHoc , :iTuSoTCTL ,	 :iDenSoTCTL)");
			query.setBigInteger("iDotDKId", dotDkId);
			query.setBigInteger("iNhomDKId", nhomid);
			query.setInteger("iTuKhoaHoc", iTuKhoaHoc);
			query.setInteger("iDenKhoaHoc", iDenKhoaHoc);
			query.setInteger("iTuSoTCTL", iTuSoTCTL);
			query.setInteger("iDenSoTCTL", iDenSoTCTL);
			query.executeUpdate();
			
		} catch (Exception e) {
			return false;
		}

		return true;
	
   }
}//endclass
